package com.ljf.order.controller;

import com.ljf.order.VO.ResultVO;
import com.ljf.order.VO.ResultVOUtil;
import com.ljf.order.converter.OrderForm2OrderDTOConverter;
import com.ljf.order.dto.OrderDTO;
import com.ljf.order.enums.ResultEnum;
import com.ljf.order.exception.SellException;
import com.ljf.order.form.OrderForm;
import com.ljf.order.service.BuyerService;
import com.ljf.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Ljf
 * @Date : 2019/2/2 16:53
 * @Description :
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private BuyerService buyerService;

    //创建订单
    @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】 参数不正确,orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR, bindingResult.getFieldError().getDefaultMessage());
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDTO.getOrderDetails())) {
            log.error("【创建订单】,购物车不可为空");
            throw new SellException(ResultEnum.CART_ERROR);
        }
        orderDTO = orderService.create(orderDTO);
        Map<String, String> map = new HashMap<>();
        map.put("orderId", orderDTO.getOrderId());
        log.info("订单创建信息是: {}",orderDTO);
        return ResultVOUtil.success(map);
    }

    //订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid, @RequestParam(value = "page", defaultValue = "0")
            Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size) {
        if (StringUtils.isEmpty(openid)) {
            log.error("【查询订单列表】 openid为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        PageRequest request = new PageRequest(page, size);
        Page<OrderDTO> pageList = orderService.findList(openid, request);
        return ResultVOUtil.success(pageList.getContent());
    }

    //订单详情
    @GetMapping("/orderDetail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid, @RequestParam(value = "orderId") String orderId) {
        OrderDTO dto = buyerService.findOrderOne(openid, orderId);
        return ResultVOUtil.success(dto);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVO<OrderDTO> cancel(@RequestParam("openid") String openid, @RequestParam(value = "orderId") String orderId) {
        buyerService.cancelOrderOne(openid, orderId);
        return ResultVOUtil.success();
    }
}
